package com.tianqu.tms.www.car.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.actionform.CarSeniorSearchInputActionForm;
import com.tianqu.tms.www.car.controller.CarInnerListTopController;
import com.tianqu.tms.www.car.controller.CarSeniorSearchResultController;
import com.tianqu.tms.www.car.viewhelper.CarSeniorSearchInputViewhelper;

/**
 * 搜索结果页
 * 
 * @author Administrator
 *
 */

public class CarSeniorSearchResultAction extends TmsAction {
	
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";

	private Logger logger = Logger.getLogger(CarSeniorSearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		CarSeniorSearchInputActionForm seniorFrom = (CarSeniorSearchInputActionForm)form;
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarSeniorSearchInputViewhelper view = new CarSeniorSearchInputViewhelper();
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, view.getPageNo());
			uc.setAttribute(SORTKEY, "9");
			uc.setAttribute(MAXINDEX, "50");
			uc.setAttribute(ASC, "false");
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		CarSeniorSearchInputActionForm seriorForm = (CarSeniorSearchInputActionForm) uc .getInputForm();
		if (uc.getAttribute("seniorFrom") != null){
			seriorForm = (CarSeniorSearchInputActionForm) uc.getAttribute("seniorFrom");
		}
		if(StringUtil.isNullOrBlank(seriorForm.getFromType()) && StringUtil.isNullOrBlank(seriorForm.getBrandId()) && 
				StringUtil.isNullOrBlank(seriorForm.getVendorId()) && StringUtil.isNullOrBlank(seriorForm.getLicensePlate()) && 
				StringUtil.isNullOrBlank(seriorForm.getVin()) && StringUtil.isNullOrBlank(seriorForm.getMemberId()) &&  
				StringUtil.isNullOrBlank(seriorForm.getStartTime()) && StringUtil.isNullOrBlank(seriorForm.getEndTime()) && 
				StringUtil.isNullOrBlank(seriorForm.getRegisterStatus()) && StringUtil.isNullOrBlank(seriorForm.getBarCodeNum())&&
				StringUtil.isNullOrBlank(seriorForm.getTempLicensePlate()) && StringUtil.isNullOrBlank(seriorForm.getMinPirce()) && StringUtil.isNullOrBlank(seriorForm.getMaxPrice())&& 
				StringUtil.isNullOrBlank(seriorForm.getStartYear()) && StringUtil.isNullOrBlank(seriorForm.getEndYear())){								
			setErrorsToRequest(request, TmsConstants.U0040 ,null);	//必须输入一个查询条件。		
			return mapping.getInputForward();			
		}
		//
		if(StringUtil.isNotBlank(seriorForm.getLicensePlate()) && seriorForm.getLicensePlate().length() != 5){
			setErrorsToRequest(request, TmsConstants.U8009, null); // 请输入车牌号码后5位
			return mapping.getInputForward();	
		}
		//
		if(StringUtil.isNotBlank(seriorForm.getVin()) && seriorForm.getVin().length() != 6){
			setErrorsToRequest(request, TmsConstants.U8010, null); //请输入车架号后6位。
			return mapping.getInputForward();	
		}
		//
		if(StringUtil.isNotBlank(seriorForm.getTempLicensePlate()) && seriorForm.getTempLicensePlate().length() != 8){
			setErrorsToRequest(request, TmsConstants.U8021, null); //请输入易碎贴编号后8位。
			return mapping.getInputForward();	
		}
		//
		if(StringUtil.isNotBlank(seriorForm.getMinPirce()) && !StringUtil.isNumeric(seriorForm.getMinPirce())){
			String[] param = {"在售价格区间（元）"};
			setErrorsToRequest(request, TmsConstants.U0015, param); 
			return mapping.getInputForward();	
		}
		if(StringUtil.isNotBlank(seriorForm.getMaxPrice()) && !StringUtil.isNumeric(seriorForm.getMaxPrice())){
			String[] param = {"在售价格区间（元）"};
			setErrorsToRequest(request, TmsConstants.U0015, param); 
			return mapping.getInputForward();	
		}
		if(StringUtil.isNotBlank(seriorForm.getMinPirce()) 
				&& StringUtil.isNotBlank(seriorForm.getMaxPrice()) 
				&& Integer.parseInt(seriorForm.getMaxPrice()) < Integer.parseInt(seriorForm.getMinPirce())){
			String[] param = {"在售价格区间（元）"};
			setErrorsToRequest(request, TmsConstants.U0043, param); 
			return mapping.getInputForward();	
		}
        //
		if(StringUtil.isNotBlank(seriorForm.getStartYear()) && !StringUtil.isNumeric(seriorForm.getStartYear())){
			String[] param = {"年限区间（年）"};
			setErrorsToRequest(request, TmsConstants.U0015, param); 
			return mapping.getInputForward();	
		}
		if(StringUtil.isNotBlank(seriorForm.getEndYear()) && !StringUtil.isNumeric(seriorForm.getEndYear())){
			String[] param = {"年限区间（年）"};
			setErrorsToRequest(request, TmsConstants.U0015, param); 
			return mapping.getInputForward();	
		}
		if(StringUtil.isNotBlank(seriorForm.getStartYear()) 
				&& StringUtil.isNotBlank(seriorForm.getEndYear()) 
				&& Integer.parseInt(seriorForm.getEndYear()) < Integer.parseInt(seriorForm.getStartYear())){
			String[] param = {"年限区间（年）"};
			setErrorsToRequest(request, TmsConstants.U0043, param); 
			return mapping.getInputForward();	
		}
		//库存登记日期/区间check
		if ((!StringUtil.isNullOrBlank(seriorForm.getStartTime()))&&(!StringUtil.isNullOrBlank(seriorForm.getEndTime()))) {	
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			//库存登记日期前段
			Date startDate = formatDate.parse(seriorForm.getStartTime());
			//库存登记日期后段				
			Date endDate = formatDate.parse(seriorForm.getEndTime());
			// 比较
			if (endDate.compareTo(startDate) < 0) {
				String[] param = {"库存登记日期/区间"};
				setErrorsToRequest(request, TmsConstants.U0038, param); //库存登记日期/区间填写的后一个日期不能比前一个日期小。
				return mapping.getInputForward();
			}
		}
		
		CarSeniorSearchResultController controller = new CarSeniorSearchResultController();
		controller.execute(uc);
		
		List list = (List)uc.getAttribute(TmsConstants.VIEWHELP);
		if(list.size() == 0){
			setErrorsToRequest(request, TmsConstants.U0039 ,null);	//查询的结果不存在。	
			return mapping.getInputForward();
		} else if(list.size() > 300){
			setErrorsToRequest(request, TmsConstants.U0041 ,null);	//查询的结果已超过300条记录，请缩小查询范围后再进行查询。	
			return mapping.getInputForward();
		}

		view.setCarSenSearchList(list);
		Car car = (Car)uc.getAttribute(CarInnerListTopController.PAGING);
		
		// viewhelp中设置翻页。
		view.setPageNo(Integer.toString(car.getPageNo()));
		view.setMaxIndex(Integer.toString(car.getMaxIndex()));
		view.setAsc(Boolean.toString(car.isAsc()));
		view.setSortKey(Integer.toString(car.getSortKey()));
		view.setTotalCount(Integer.toString(car.getTotalCount()));
		view.setDateFrom(Integer.toString(car.getDateFrom()));
		view.setDateTo(Integer.toString(car.getDateTo()));
		
		
		request.setAttribute(TmsConstants.VIEWHELP, view);//直接把list打到前台
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
